package io.adrainty.bolee.insurance.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.adrainty.bolee.insurance.mapper.CustomerRelationMapper;
import io.adrainty.bolee.insurance.models.bo.CustomerRelationBO;
import io.adrainty.bolee.insurance.models.entity.CustomerRelationEntity;
import io.adrainty.bolee.insurance.repository.ICustomerRelationRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/24 下午11:40
 */

@Repository
public class CustomerRelationRepositoryImpl implements ICustomerRelationRepository {

    @Resource
    private CustomerRelationMapper customerRelationMapper;

    @Override
    public List<CustomerRelationEntity> listByIds(List<String> customerRelationIds) {
        if (CollUtil.isEmpty(customerRelationIds)) return List.of();
        List<CustomerRelationBO> customerRelationBOS = customerRelationMapper.selectBatchIds(customerRelationIds);
        return BeanUtil.copyToList(customerRelationBOS, CustomerRelationEntity.class);
    }

    @Override
    public List<CustomerRelationEntity> findList(CustomerRelationEntity relationEntity) {
        Wrapper<CustomerRelationBO> wrapper = getWrapper(relationEntity);
        List<CustomerRelationBO> customerRelationBOS = customerRelationMapper.selectList(wrapper);
        return BeanUtil.copyToList(customerRelationBOS, CustomerRelationEntity.class);
    }

    private Wrapper<CustomerRelationBO> getWrapper(CustomerRelationEntity relationEntity) {
        return Wrappers.lambdaQuery(CustomerRelationBO.class)
                .eq(relationEntity.getCustomerId() != null, CustomerRelationBO::getCustomerId, relationEntity.getCustomerId())
                .eq(StringUtils.isNotBlank(relationEntity.getRelation()), CustomerRelationBO::getRelation, relationEntity.getRelation())
                .eq(relationEntity.getName() != null, CustomerRelationBO::getName, relationEntity.getName())
                .eq(StringUtils.isNotBlank(relationEntity.getIdentityCard()), CustomerRelationBO::getIdentityCard, relationEntity.getIdentityCard())
                .eq(StringUtils.isNotBlank(relationEntity.getCityNo()), CustomerRelationBO::getCityNo, relationEntity.getCityNo())
                .eq(relationEntity.getDataState() != null, CustomerRelationBO::getDataState, relationEntity.getDataState())
                .eq(StringUtils.isNotBlank(relationEntity.getCompanyNo()), CustomerRelationBO::getCompanyNo, relationEntity.getCompanyNo())
                .orderByAsc(CustomerRelationBO::getSortNo, CustomerRelationBO::getCreatedTime);
    }

}
